package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.sys.models.bo.SysWebsiteSeoSaveBO;
import com.ztgf.sys.models.dto.SysWebsiteSeoDTO;
import com.ztgf.sys.models.service.ISysWebsiteSeoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 网站SEO控制器
 *
 * @author YUE
 * @Date 2020-07-09 11:22:10
 */

@RestController
@RequestMapping("/cms/sysWebsiteSeo")
@Slf4j
@Api(value = "SysWebsiteSeoController",tags = "网站SEO接口")
public class SysWebsiteSeoController {

    @DubboReference
    private ISysWebsiteSeoService sysWebsiteSeoService;

    @ApiOperation(value = "添加网站SEO",notes = "添加网站SEO")
    @PostMapping(value = "/insert")
    public Result insert(SysWebsiteSeoSaveBO saveBO) {
        boolean flag = sysWebsiteSeoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存网站SEO",notes = "保存网站SEO")
    public Result save(SysWebsiteSeoSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改网站SEO",notes = "修改网站SEO")
    @PostMapping("/update")
    public Result update(SysWebsiteSeoSaveBO saveBO){
        SysWebsiteSeoDTO dto = new SysWebsiteSeoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = sysWebsiteSeoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除网站SEO",notes = "删除网站SEO")
    @ApiImplicitParam(name = "id",value = "网站SEOid",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = sysWebsiteSeoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    /**
     * 获取网站seo
     * @return
     */
    @PostMapping("/get")
    public Result<SysWebsiteSeoDTO> get(){
        List<SysWebsiteSeoDTO> websiteSeoDTOList = sysWebsiteSeoService.selectList(new EntityWrapper<>());
        if(websiteSeoDTOList.size() <=0){
            return new Result<>();
        }
        return new Result<>(websiteSeoDTOList.get(0));
    }
}
